//
//  HMComposeView.swift
//  Weibo
//
//  Created by ZhuDong on 16/5/28.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMComposeView: UIView {
    var com_btns: [HMComposeButton] = [HMComposeButton]()
    weak var target: UIViewController?
    func show(targetVC target: UIViewController){
        self.target = target
        target.view.addSubview(self)
    }
    override init(frame: CGRect) {
        let window = UIApplication.sharedApplication().keyWindow
        let rect = window?.bounds
        super.init(frame: rect!)
        self.backgroundColor = UIColor.redColor()
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
        addSubview(imageView)
        addSubview(logoView)
        logoView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(100)
        }
        addBtns()
    }
    func addBtns(){
        let h: CGFloat = 120
        let w: CGFloat = 80
        let margin = (ScreenWidth - 3 * w) / 4
        for item in 0..<composeItems.count {
            let com_item = composeItems[item]
            let btn = HMComposeButton()
            btn.composeItem = com_item
            btn.setTitle(com_item.title, forState: .Normal)
            btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
            btn.titleLabel?.font = UIFont.systemFontOfSize(14)
            btn.titleLabel?.textAlignment = .Center
            btn.setImage(UIImage(named: com_item.icon!), forState: .Normal)
            btn.addTarget(self, action: #selector(btnClick(_:)), forControlEvents: .TouchUpInside)
            addSubview(btn)
            
            self.com_btns.append(btn)
            let row = CGFloat(item / 3)
            let col = CGFloat(item % 3)
            let x = margin + (margin + w) * col
            let y = (margin + h) * row + ScreenHeigh
            let rect = CGRect(x: 0, y: 0, width: w, height: h)
            btn.frame = rect.offsetBy(dx: x, dy: y)
        }
        
    }
    @objc func btnClick(btn: HMComposeButton){
        UIView.animateWithDuration(0.25, animations: {
            var scale: CGFloat = 0
            for item in self.com_btns {
                if item == btn {
                    scale = 2
                }else{
                    scale = 0.2
                }
                item.transform = CGAffineTransformMakeScale(scale, scale)
                item.alpha = 0
            }
            }) { (_) in
//                btn.alpha = 0
            //跳转控制器
                guard let VC = btn.composeItem?.target else {
                    return
                }
                let type = NSClassFromString(VC) as! UIViewController.Type
                let targetVC = type.init()
                let nav = UINavigationController(rootViewController: targetVC)
                self.target?.presentViewController(nav, animated: true, completion: { _ in
                    self.removeFromSuperview()
                })
        }
  
    }
    override func willMoveToSuperview(newSuperview: UIView?) {
        super.willMoveToSuperview(newSuperview)
        startAnimation(-350,btns: self.com_btns)
    }
    private func startAnimation(offset: CGFloat,btns: [HMComposeButton]){
        for (index,item) in btns.enumerate() {
            UIView.animateWithDuration(0.8, delay: Double(index) * 0.04, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: [], animations: {
                var center = item.center
                center.y += offset
                item.center = center
                }, completion: { _ in
                    if(offset > 0){
                        self.removeFromSuperview()
                    }
            })
        }
    }
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        startAnimation(350, btns: self.com_btns.reverse())
    }
    private lazy var imageView: UIImageView = {
        let image:UIImage = UIImage().snapShotCurrentImage().applyLightEffect()
        let imv = UIImageView(image: image)
        return imv
    }()
    private lazy var logoView: UIImageView = {
        let image = UIImage(named: "compose_slogan")
        let imageView = UIImageView(image: image)
        return imageView
    }()
    private lazy var composeItems: [HMComposeItem] = HMComposeItem.composeItems()
}
